package com.itheima.demo3.service;

import com.itheima.demo3.dto.LoginRequest;
import com.itheima.demo3.dto.RegisterRequest;
import com.itheima.demo3.entity.User;
import com.itheima.demo3.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 用户服务层
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserService {
    
    private final UserMapper userMapper;
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    
    /**
     * 用户注册
     * @param request 注册请求
     * @return 注册结果
     */
    public String register(RegisterRequest request) {
        // 检查用户名是否已存在
        User existingUser = userMapper.findByUsername(request.getUsername());
        if (existingUser != null) {
            return "Username already exists";
        }
        
        // 检查邮箱是否已存在
        if (request.getEmail() != null && !request.getEmail().isEmpty()) {
            User existingEmailUser = userMapper.findByEmail(request.getEmail());
            if (existingEmailUser != null) {
                return "Email already exists";
            }
        }
        
        // 创建新用户
        User user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(passwordEncoder.encode(request.getPassword())); // 加密密码
        user.setEmail(request.getEmail());
        user.setPhone(request.getPhone());
        user.setNickname(request.getNickname() != null ? request.getNickname() : request.getUsername());
        user.setStatus(1); // 默认状态为正常
        
        int result = userMapper.insert(user);
        if (result > 0) {
            log.info("User registered successfully: {}", request.getUsername());
            return "User registered successfully";
        } else {
            log.error("Failed to register user: {}", request.getUsername());
            return "Failed to register user";
        }
    }
    
    /**
     * 用户登录
     * @param request 登录请求
     * @return 登录结果
     */
    public String login(LoginRequest request) {
        // 根据用户名查询用户
        User user = userMapper.findByUsername(request.getUsername());
        if (user == null) {
            return "User not found";
        }
        
        // 检查密码
        if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
            return "Invalid password";
        }
        
        // 检查用户状态
        if (user.getStatus() != 1) {
            return "User account is disabled";
        }
        
        log.info("User logged in successfully: {}", request.getUsername());
        return "Login successful";
    }
    
    /**
     * 根据用户名查询用户信息
     * @param username 用户名
     * @return 用户信息
     */
    public User getUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }
    
    /**
     * 根据用户ID查询用户信息
     * @param id 用户ID
     * @return 用户信息
     */
    public User getUserById(Long id) {
        return userMapper.findById(id);
    }
} 